package com.nanako.controller;

import com.nanako.entity.Cash;
import com.nanako.service.StaticsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;

@Slf4j
@RestController
@RequestMapping("/statics")
public class StaticsController {
    @Autowired
    private StaticsService staticsService;



    @GetMapping("/dishCash")
    public Map<Object, Object> dishCash() {
        List<Cash> maps = staticsService.dishCash();
        HashMap<Object, Object> hashMap = new HashMap<>();
        hashMap.put("dishCategoryCash", maps);
        return hashMap;
    }

    @GetMapping("/setmealCash")
    public Map<Object, Object> setmealCash() {
        List<Cash> maps = staticsService.setmealCash();
        HashMap<Object, Object> hashMap = new HashMap<>();
        hashMap.put("setmealCash", maps);
        return hashMap;
    }

    @GetMapping("/dishNum")
    public Map<Object, Object> dishNum() {
        List<Cash> maps = staticsService.dishNum();
        HashMap<Object, Object> hashMap = new HashMap<>();
        hashMap.put("dishNum", maps);
        return hashMap;
    }
    @GetMapping("/setmealNum")
    public Map<Object, Object> setmealNum() {
        List<Cash> maps = staticsService.setmealNum();
        HashMap<Object, Object> hashMap = new HashMap<>();
        hashMap.put("setmealNum", maps);
        return hashMap;
    }
    @GetMapping("/cashPerMonth")
    public Map<Object, Object> cashPerMonth() {
        Map<Object, Object> map = staticsService.cashPerMonth();
        LocalDate localDate = LocalDate.now();
        String date = "";
        LinkedList<String> dates = new LinkedList<>();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM");
        for (int i = 0; i < 12; i++) {
            date = localDate.format(formatter);
            dates.push(date);
            localDate = localDate.minusMonths(1L);
        }
        map.put("monthList", dates);
        return map;
    }
}
